package com.right.health.app.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.base.BasePresenterActivity;
import com.right.health.app.bean.AppUserResponse;
import com.right.health.app.bean.UploadResponse;
import com.right.health.app.common.DataCommon;
import com.right.health.app.constants.AppConfig;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.dialog.PhotoDialog;
import com.right.health.app.dialog.ProfileCertificationDialog;
import com.right.health.app.net.UploadApi;
import com.right.health.app.net.UserApi;
import com.right.health.app.utils.ChoosePhotoUtil;
import com.right.health.app.utils.GlideUtil;

/*
 * Created by hopeliao on 2019-09-27
 *
 */
public class ProfileActivity extends BasePresenterActivity implements View.OnClickListener,
        ChoosePhotoUtil.OnChooseImageCallback, OnHttpListener {

    private static final int HTTP_CODE_GET_USER_INFO = 1;
    private static final int HTTP_CODE_UPLOAD_FILE = 2;
    private static final int HTTP_CODE_UPLOAD_USER = 3;

    private ProfileCertificationDialog mCertificationDialog;
    private PhotoDialog mPhotoDialog;

    private ChoosePhotoUtil mChoosePhotoUtil;

    private ImageView mAvatarImage;

    private UserApi mUserApi;

    private AppUserResponse.AppUserInfo mUserInfo;

    private UploadApi mUploadApi;

    private TextView mNameTv, mSexTv, mIdcardTv, mPhoneTv, mVertificationTv;
    private TextView mAppNumberTv;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.profile);
    }

    @Override
    public void findView() {
        findViewById(R.id.rl_certification_item).setOnClickListener(this);
        findViewById(R.id.rl_avatar_item).setOnClickListener(this);

        mPhoneTv = (TextView) findViewById(R.id.tv_phone);
        mAvatarImage = (ImageView) findViewById(R.id.iv_avatar);
        mNameTv = (TextView) findViewById(R.id.tv_name);
        mSexTv = (TextView) findViewById(R.id.tv_sex);
        mIdcardTv = (TextView) findViewById(R.id.tv_idcard);
        mVertificationTv = (TextView) findViewById(R.id.tv_vertification);
        mAppNumberTv = (TextView) findViewById(R.id.tv_app_num);
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);

        mUploadApi = new UploadApi();
        mUploadApi.setListener(this);

        mChoosePhotoUtil = new ChoosePhotoUtil(this);
        mChoosePhotoUtil.setCallback(this);
        mChoosePhotoUtil.setCropParam(200, 200);

        mUserInfo = App.getInstance().getUser();
        refreshUI();
        mUserApi.addRequestCode(HTTP_CODE_GET_USER_INFO);
        mUserApi.getUserInfo();
    }

    private void refreshUI() {
        if (mUserInfo != null) {
            Glide.with(App.getInstance()).load(AppConfig.PHOTO_URL + mUserInfo.imageurl).apply(GlideUtil.getRoundOption()).into(mAvatarImage);

            mNameTv.setText(mUserInfo.personname);
            mSexTv.setText(DataCommon.getSex(mUserInfo.sex));
            mIdcardTv.setText(mUserInfo.idcard);
            mPhoneTv.setText(mUserInfo.mobilephone);

            if (mUserInfo.isVertificationSucc()) {
                mVertificationTv.setTextColor(getResources().getColor(R.color.color_09C3BE));
                mVertificationTv.setText("已认证");
            } else {
                mVertificationTv.setTextColor(getResources().getColor(R.color.color_E60012));
                mVertificationTv.setText("未认证");
            }

            mAppNumberTv.setText(mUserInfo.healthcardno);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_certification_item:
                if (TextUtils.equals(App.getInstance().getUser().verifystatus, "1")) {
                    ToastUtil.show(this, R.string.me_already_authenticated);
                } else {
                    if (mCertificationDialog == null) {
                        mCertificationDialog = new ProfileCertificationDialog(this);
                        mCertificationDialog.setClickListener(this);
                    }
                    mCertificationDialog.show();
                }
                break;
            case R.id.tv_choose_type1: //医院预留信息认证
                startIntent(CertificationByHospitalActivity.class);
                dismiss();
                break;
            case R.id.tv_choose_type2: //身份证自动认证
                startIntent(CertificationByCardActivity.class);
                dismiss();
                break;
            case R.id.tv_choose_type3: //人工审核认证
                startIntent(CertificationByArtificialActivity.class);
                dismiss();
                break;
            case R.id.rl_avatar_item: //头像点击
                if (mPhotoDialog == null) {
                    mPhotoDialog = new PhotoDialog(this);
                    mPhotoDialog.setClickListener(this);
                }
                mPhotoDialog.show();
                break;
            case R.id.tv_choose_camera://相机
                mChoosePhotoUtil.choosePhoto();
                dismissPhotoDialog();
                break;
            case R.id.tv_choose_album: //相册
                mChoosePhotoUtil.chooseAlbum();
                dismissPhotoDialog();
                break;
        }
    }

    private void dismiss() {
        if (mCertificationDialog != null && mCertificationDialog.isShowing()) {
            mCertificationDialog.dismiss();
        }
    }

    private void dismissPhotoDialog() {
        if (mPhotoDialog != null && mPhotoDialog.isShowing()) {
            mPhotoDialog.dismiss();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mChoosePhotoUtil.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onResult(String filePath) {
        if (!TextUtils.isEmpty(filePath)) {
            mUploadApi.addRequestCode(HTTP_CODE_UPLOAD_FILE);
            mUploadApi.uploadFile(filePath);

        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_UPLOAD_USER) {
                ToastUtil.show(this, "修改成功");
                sendBroadcast(BroadCastCode.REFRESH_USER);
                if(mUserInfo != null) {
                    App.getInstance().refreshUser(mUserInfo);
                }
            } else if (request.requestCode == HTTP_CODE_UPLOAD_FILE) {
                if (response.targetData instanceof UploadResponse) {
                    UploadResponse uploadResponse = response.cast(UploadResponse.class);
                    if (CollectionUtil.isEmpty(uploadResponse.imgPath)) {
                        return;
                    }
                    String imageServerPath = uploadResponse.imgPath.get(0);
                    mUserInfo.imageurl = imageServerPath;
                    refreshUI();

                    mUserApi.addRequestCode(HTTP_CODE_UPLOAD_USER);
                    mUserApi.updateUserInfo(imageServerPath);
                }
            } else if (request.requestCode == HTTP_CODE_GET_USER_INFO) {
                if (response.targetData instanceof AppUserResponse) {
                    AppUserResponse appUserResponse = response.cast(AppUserResponse.class);
                    App.getInstance().refreshUser(appUserResponse.user);

                    mUserInfo = appUserResponse.user;
                    refreshUI();
                }
            }
        }
    }
}
